// set function parseTime,formatTime to filter
export { parseTime, formatTime } from '@/utils'
import dayJs from 'dayjs'

/**
 * Show plural label if time is plural number
 * @param {number} time
 * @param {string} label
 * @return {string}
 */
function pluralize(time, label) {
  if (time === 1) {
    return time + label
  }
  return time + label + 's'
}

/**
 * @param {number} time
 */
export function timeAgo(time) {
  const between = Date.now() / 1000 - Number(time)
  if (between < 3600) {
    return pluralize(~~(between / 60), ' minute')
  } else if (between < 86400) {
    return pluralize(~~(between / 3600), ' hour')
  } else {
    return pluralize(~~(between / 86400), ' day')
  }
}

/**
 * Number formatting
 * like 10000 => 10k
 * @param {number} num
 * @param {number} digits
 */
export function numberFormatter(num, digits) {
  const si = [
    { value: 1E18, symbol: 'E' },
    { value: 1E15, symbol: 'P' },
    { value: 1E12, symbol: 'T' },
    { value: 1E9, symbol: 'G' },
    { value: 1E6, symbol: 'M' },
    { value: 1E3, symbol: 'k' }
  ]
  for (let i = 0; i < si.length; i++) {
    if (num >= si[i].value) {
      return (num / si[i].value + 0.1).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol
    }
  }
  return num.toString()
}

/**
 * 10000 => "10,000"
 * @param {number} num
 */
export function toThousandFilter(num) {
  return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
}
// 申请单状态
export function sheetStatus(num) {
  const obj = {
    0: '未提交',
    1: '审批中',
    2: '已审批',
    3: '驳回',
    4: '被引用',
    5: '已释放'
  }
  return obj[num]
}
// 数据来源
export function dataSource(num) {
  const obj = {
    0: '手工录入',
    1: '凭证导入'
  }
  return obj[num]
}

// 属性
export function preset(num) {
  const obj = {
    0: '手工录入',
    1: '系统预置'
  }
  return obj[num]
}

// 是否末级
export function isDetail(num) {
  const obj = {
    0: '否',
    1: '是'
  }
  return obj[num * 1]
}
// 控制粒度
export function Dimension(num) {
  const obj = {
    0: '业务',
    1: '归口'
  }
  return obj[num * 1]
}
// 控制粒度
export function Grad(num) {
  const obj = {
    1: '年度',
    4: '月度'
  }
  return obj[num * 1]
}

// 启用停用
export function State(num) {
  const obj = {
    0: '启用',
    1: '停用'
  }
  return obj[num * 1]
}
// 日期格式化
export function formatDate(date, str = 'YYYY-MM-DD') {
  return date ? dayJs(date).format(str) : ''
}
// 不处理过滤
export function noop(val) {
  return val
}
